package com.itheima.work;

import java.util.Scanner;
import java.util.TreeSet;

/**
 * @auther Macrydy
 * @date 2020/6/28 15:54
 * 键盘录入字符串,统计里面每个字符串出现的次数
 */
public class Test2 {


    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个字符串");
        String s = scanner.nextLine();
        char[] chars= s.toCharArray();

        //创建set去除chars数组中重复元素
        TreeSet<Character> set = new TreeSet<>();
        for (char aChar : chars) {
            set.add(aChar);
        }
        for (Character character : set) {
            int num = method(character, chars);
            System.out.println("字符"+character+"出现的次数为："+num);
        }

    }
    //将每个字符与数组中每个元素进行比较，相同则数量+1
    public  static int method(Character char1,char[] chars){
        int num=0;
        for (char aChar : chars) {
            if (char1.equals(aChar)){
                num++;
            }
        }
        return num;
    }
}
